/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import com.applicatioN.entities.Exiger4;
import com.applicatioN.entities.Exiger4PK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Specialite;
import com.applicatioN.entities.Poste;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class Exiger4JpaController implements Serializable {

    public Exiger4JpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Exiger4 exiger4) throws PreexistingEntityException, Exception {
        if (exiger4.getExiger4PK() == null) {
            exiger4.setExiger4PK(new Exiger4PK());
        }
        exiger4.getExiger4PK().setSpecialite(exiger4.getSpecialite1().getSpecialite());
        exiger4.getExiger4PK().setNomPoste(exiger4.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specialite specialite1 = exiger4.getSpecialite1();
            if (specialite1 != null) {
                specialite1 = em.getReference(specialite1.getClass(), specialite1.getSpecialite());
                exiger4.setSpecialite1(specialite1);
            }
            Poste poste = exiger4.getPoste();
            if (poste != null) {
                poste = em.getReference(poste.getClass(), poste.getNomPoste());
                exiger4.setPoste(poste);
            }
            em.persist(exiger4);
            if (specialite1 != null) {
                specialite1.getExiger4Collection().add(exiger4);
                specialite1 = em.merge(specialite1);
            }
            if (poste != null) {
                poste.getExiger4Collection().add(exiger4);
                poste = em.merge(poste);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findExiger4(exiger4.getExiger4PK()) != null) {
                throw new PreexistingEntityException("Exiger4 " + exiger4 + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Exiger4 exiger4) throws NonexistentEntityException, Exception {
        exiger4.getExiger4PK().setSpecialite(exiger4.getSpecialite1().getSpecialite());
        exiger4.getExiger4PK().setNomPoste(exiger4.getPoste().getNomPoste());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger4 persistentExiger4 = em.find(Exiger4.class, exiger4.getExiger4PK());
            Specialite specialite1Old = persistentExiger4.getSpecialite1();
            Specialite specialite1New = exiger4.getSpecialite1();
            Poste posteOld = persistentExiger4.getPoste();
            Poste posteNew = exiger4.getPoste();
            if (specialite1New != null) {
                specialite1New = em.getReference(specialite1New.getClass(), specialite1New.getSpecialite());
                exiger4.setSpecialite1(specialite1New);
            }
            if (posteNew != null) {
                posteNew = em.getReference(posteNew.getClass(), posteNew.getNomPoste());
                exiger4.setPoste(posteNew);
            }
            exiger4 = em.merge(exiger4);
            if (specialite1Old != null && !specialite1Old.equals(specialite1New)) {
                specialite1Old.getExiger4Collection().remove(exiger4);
                specialite1Old = em.merge(specialite1Old);
            }
            if (specialite1New != null && !specialite1New.equals(specialite1Old)) {
                specialite1New.getExiger4Collection().add(exiger4);
                specialite1New = em.merge(specialite1New);
            }
            if (posteOld != null && !posteOld.equals(posteNew)) {
                posteOld.getExiger4Collection().remove(exiger4);
                posteOld = em.merge(posteOld);
            }
            if (posteNew != null && !posteNew.equals(posteOld)) {
                posteNew.getExiger4Collection().add(exiger4);
                posteNew = em.merge(posteNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Exiger4PK id = exiger4.getExiger4PK();
                if (findExiger4(id) == null) {
                    throw new NonexistentEntityException("The exiger4 with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Exiger4PK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exiger4 exiger4;
            try {
                exiger4 = em.getReference(Exiger4.class, id);
                exiger4.getExiger4PK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The exiger4 with id " + id + " no longer exists.", enfe);
            }
            Specialite specialite1 = exiger4.getSpecialite1();
            if (specialite1 != null) {
                specialite1.getExiger4Collection().remove(exiger4);
                specialite1 = em.merge(specialite1);
            }
            Poste poste = exiger4.getPoste();
            if (poste != null) {
                poste.getExiger4Collection().remove(exiger4);
                poste = em.merge(poste);
            }
            em.remove(exiger4);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Exiger4> findExiger4Entities() {
        return findExiger4Entities(true, -1, -1);
    }

    public List<Exiger4> findExiger4Entities(int maxResults, int firstResult) {
        return findExiger4Entities(false, maxResults, firstResult);
    }

    private List<Exiger4> findExiger4Entities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Exiger4.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Exiger4 findExiger4(Exiger4PK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Exiger4.class, id);
        } finally {
            em.close();
        }
    }

    public int getExiger4Count() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Exiger4> rt = cq.from(Exiger4.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
